var tab = {};

function onClick(e) {
    var n = document.createElement('div');
    n.className = 'glasspane';
    document.body.insertBefore(n, document.body.firstElementChild);
    chrome.extension.getBackgroundPage().extension.exports[e.target.id](tab);
    window.close();
}

document.addEventListener('DOMContentLoaded', function() {
    var extension = chrome.extension.getBackgroundPage().extension;
    var body = document.body;
    var settings = extension.exports.getSettings();
    var optionsview = settings.get('optionsview');

    if (optionsview == 0)
        body.classList.add('simple');
    else if (optionsview == 1)
        body.classList.add('moderate');
    else
        body.classList.add('advanced');

    chrome.tabs.query({lastFocusedWindow: true, active: true}, function(t) {
        tab.id = t[0].id;
        var frame = extension.exports.getFrameInfo(tab);
        if (!settings.get('enabled'))
            $('toggleExtension').classList.add('checked');
        else {
            if (frame.status == 'allow') {
                $('toggleWhitelist').textContent = 'Whitelist this website';
                $('toggleWhitelist').classList.add('checked');
                body.classList.add('filtered');
            }
            else if (frame.status == 'deny') {
                $('toggleBlacklist').textContent = 'Blacklist this website';
                $('toggleBlacklist').classList.add('checked');
                body.classList.add('filtered');
            }
            else if (frame.status == 'destroy') {
            }
            else {
                if (!frame.blocked)
                    $('togglePage').classList.add('checked');
                if (settings.get('defaultmode') ? !frame.session : frame.session)
                    $('toggleSession').classList.add('checked');
            }
        }
        if (!extension.utils.permitted(t[0].url))
            body.classList.add('disable');
        body.classList.add(frame.status);
        relayEvent(body, 'click', '.menuitem', onClick);
    });
});
